package com.md.api.service.impl;

import com.md.api.common.utils.DistanceUtils;
import com.md.api.common.utils.IdUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.common.utils.TokenUtil;
import com.md.api.dao.MotOrderMapper;
import com.md.api.dao.SiteEvaluateMapper;
import com.md.api.dao.SiteInfoMapper;
import com.md.api.dao.SiteServiceContentMapper;
import com.md.api.entity.HttpResult;
import com.md.api.entity.SiteEvaluate;
import com.md.api.entity.SiteInfo;
import com.md.api.entity.SiteServiceContent;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.util.Page;
import com.md.api.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author liujie
 * @ClassName ProductServiceImpl
 **/

@Slf4j
@Service
public class SiteServiceImpl implements SiteService {


    @Resource
    SiteInfoMapper siteInfoMapper;
    @Resource
    MotOrderMapper motOrderMapper;
    @Resource
    SiteEvaluateMapper evaluateMapper;
    @Resource
    SiteServiceContentMapper serviceContentMapper;
    @Resource
    TokenUtil tokenUtil;

    @Override
    public HttpResult page(ComposeQueryDto qto) {
        Page page = qto.getPage();
        page.initPagingData(SiteInfo.class);
        List<SiteInfo> siteInfos = siteInfoMapper.selectAll(qto.getKeyword(),qto.getLatitude(),qto.getLongitude(),qto.getOrderType(),qto.getSiteType());
        page.setResult(siteInfos);
        return new HttpResult(page);
    }


    @Override
    public HttpResult detail(ComposeQueryDto info) {
        SiteInfo siteId = siteInfoMapper.getBySiteId(info.getSiteId());
        if (null!=siteId.getLatitude()&&null!=siteId.getLongitude()&&null!=info.getLatitude()&&null!=info.getLongitude()) {
            siteId.setDistance(DistanceUtils.haversineDistance(siteId.getLatitude(), siteId.getLongitude(), info.getLatitude(), info.getLongitude()));
        }
        return new HttpResult(siteId);
    }

    @Override
    public HttpResult serviceContentPage(ComposeQueryDto qto) {
        Page page = qto.getPage();
        page.initPagingData(SiteServiceContent.class);
        List<SiteServiceContent> serviceContents = serviceContentMapper.selectAll(qto.getSiteId());
        page.setResult(serviceContents);
        return new HttpResult(page);
    }

    @Override
    public HttpResult evaluatePage(ComposeQueryDto qto) {
        Page page = qto.getPage();
        page.initPagingData(SiteEvaluate.class);
        List<SiteEvaluate> evaluates = evaluateMapper.selectAll(qto.getSiteId(),qto.getStarNumStart(),qto.getStarNumEnd());
        page.setResult(evaluates);
        return new HttpResult(page);
    }


    @Override
    public HttpResult evaluate(HttpServletRequest request, SiteEvaluate evaluate) {
        String evaluateId = IdUtil.uuid();
        evaluate.setEvaluateId(evaluateId);
        String userId = tokenUtil.getUserId(request);
        evaluate.setUserId(userId);
        evaluateMapper.insert(evaluate);
        evaluateMapper.updateScore(evaluate.getSiteId());
        if (StringUtils.isNotEmpty(evaluate.getMotNo())){
            motOrderMapper.hasEvaluate(evaluate.getMotNo());
        }
        return HttpResult.OK;
    }
}
